package com.kye.express.business.jd.after_sale.fragment;

import com.kye.express.business.jd.order_detail.model.JDOrderDetailModel;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.IView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.jd.order_details.JDGoodsAfterSaleTypeBean;
import com.kye.model.bean.jd.order_details.request.RequestGoodsAfterSaleTypes;
import com.kye.model.bean.user.User;
import com.kye.model.service.JDService;

import java.util.List;

import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 18/12/2017
 * @updated : 18/12/2017
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public interface JDAfterSaleApplyConstract {

    interface JDAfterSaleApplyView extends IView {

        void onUser(User user);

        void onLoading();

        void onLoaded();

        void getAfterSaleTypesSuccess(List<JDGoodsAfterSaleTypeBean> jdGoodsAfterSaleTypeBeans);

        void getAfterSaleTypesNetFailed(String msg);

        void getAfterSaleTypesServerFailed(String message);

    }

    class JDAfterSaleApplyPresenter extends BasePresenter<JDAfterSaleApplyView> {

        private JDService jdService;

        public JDAfterSaleApplyPresenter() {
            jdService = RetrofitManager.getInstance().createOpenApiService(JDService.class);
        }

        @Override
        public void bind(JDAfterSaleApplyView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onUser(user);
                        }
                    });
        }

        public void getAfterSaleTypes(JDOrderDetailModel orderDetailModel, User userInfo) {
            Single.just(new RequestGoodsAfterSaleTypes())
                    .doOnSuccess(new Consumer<RequestGoodsAfterSaleTypes>() {
                        @Override
                        public void accept(RequestGoodsAfterSaleTypes requestGoodsAfterSaleTypes) throws Exception {
                            requestGoodsAfterSaleTypes.setJdOrderId(Long.parseLong(orderDetailModel.getOrderCode()));
                            requestGoodsAfterSaleTypes.setPhone(userInfo.getPhone());
                            requestGoodsAfterSaleTypes.setSkuId(Integer.parseInt(orderDetailModel.getSkuId()));
                        }
                    })
                    .flatMap(new Function<RequestGoodsAfterSaleTypes, SingleSource<OpenApiResult<List<JDGoodsAfterSaleTypeBean>>>>() {
                        @Override
                        public SingleSource<OpenApiResult<List<JDGoodsAfterSaleTypeBean>>> apply(RequestGoodsAfterSaleTypes requestGoodsAfterSaleTypes) throws Exception {
                            return jdService.getGoodsAfterSaleTypes(requestGoodsAfterSaleTypes)
                                    .compose(getView().bindToLifecycleDestroy());
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<List<JDGoodsAfterSaleTypeBean>>() {
                        @Override
                        public void onLoading(Disposable d) {

                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<List<JDGoodsAfterSaleTypeBean>> result, List<JDGoodsAfterSaleTypeBean> jdGoodsAfterSaleTypeBeans) {
                            getView().getAfterSaleTypesSuccess(jdGoodsAfterSaleTypeBeans);
                        }

                        @Override
                        public void onApiError(OpenApiResult<List<JDGoodsAfterSaleTypeBean>> result, int code, String msg) {
                            getView().getAfterSaleTypesNetFailed(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().getAfterSaleTypesServerFailed(e.getMessage());
                        }
                    });
        }
    }
}
